import { exportExcel } from '@/credit/mixin/taskTableProvinceUnion'
import { mapGetters } from 'vuex'

/**
 * 外部必须定义的数据：
 *
 * data
 *   - tableColumns
 *   - tableName
 *   - order
 *   - params
 *
 * methods
 *   - dataAdapter(row)
 *
 */
export default {
  props: {
    type: String,
    status: String,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      totalCount: 0,
      exportLoading: false,
      dialogHeight: 300,
      dateFmt: 'yyyy-MM-dd hh:mm',
      tableName: 'datum_customer_info_task',
      mangeParams: {},
    }
  },
  computed: {
    ...mapGetters(['clientId', 'userId']),
  },
  watch: {},
  created() {
    this.mangeParams['task_type.in'] = this.type
    this.mangeParams['task_status.eq'] = this.status
    const taskPerson = [this.clientId]
    if (+this.type === 3) {
      taskPerson.push('all-db')
      this.mangeParams.address_code = this.$utils.getAddressCode()
    }
    this.mangeParams['orWhere.task_person.in'] = taskPerson.join(',')
    this.mangeParams['orWhere.task_person.like'] = this.userId
    this.dialogHeight = Math.max(300, window.innerHeight - 310)
  },
  methods: {
    getParams() {
      if (+this.status === 1) {
        this.mangeParams.groupFields = ['customer_related_id'] // 去重
      }
      return {
        ...this.params,
        ...this.mangeParams,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, params, this.order)
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.totalCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    // async handleEditSuccess() {
    //   this.$store.dispatch('getMyTaskCount')
    //   this.getTabData()
    // },
    async exportExcel(filename) {
      this.exportLoading = true
      exportExcel({
        filename,
        tableColumns: this.tableColumns,
        params: this.getParams(),
        dataAdapter: this.dataAdapter,
        fetchData: this.fetchData,
        onLoaded: () => {
          this.exportLoading = false
        },
      })
    },
  },
}
